<?php
/**
 * Pertyler Classes
 * Page Class
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

class Page
{
	private $name;

	private $parameterArray = array();

	public function __Construct($name)
	{
		$this->name = (string) $name;
	}

	public function __ToString()
	{
		return $this->name;
	}

	public function __Get($parameter)
	{
		if($this->__Isset($parameter))
			return $this->parameterArray[$parameter];
		else
			return NULL;
	}

	public function __Isset($parameter)
	{
		if((empty($this->parameterArray[$parameter])) AND (isset($_GET[$parameter])) AND (Link::ParameterControl($_GET[$parameter])))
			$this->parameterArray[$parameter] =& $_GET[$parameter];

		return isset($this->parameterArray[$parameter]);
	}

	public function __Set($parameter, $value)
	{
		if(Link::ParameterControl($value))
		{
			$this->parameterArray[$parameter] = $value;

			return TRUE;
		}
		else
			return FALSE;
	}

	public function __Unset($parameter)
	{
		if($this->__Isset($parameter))
		{
			unset($this->parameterArray[$parameter]);

			return TRUE;
		}
		else
			return FALSE;
	}

	public function HTML($hTML)
	{
		return '<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >

<html xmlns="http://www.w3.org/1999/xhtml" >
	<head >
		<title >' . $this->name . '</title>
	</head>

	<body >' . $hTML . '</body>
</html>';
	}

	public function CodeToHTML($path)
	{
		if(file_exists($path))
			return $this->HTML('<div >' . highlight_file($path, true) . '</div>');
		else
			return NULL;
	}

	public function HREF()
	{
		if($this->parameterArray)
		{
			$href = '?';

			$count = 0;

			foreach($this->parameterArray AS $parameter => $value)
			{
				if($count++)
					$href .= '&';

				$href .= $parameter . '=' . $value;
			}

			return $href;
		}
		else
			return NULL;
	}

	public function Tree($name = NULL, $property = NULL)
	{
		if(!$name)
			$name = $this->name;
		else
			$this->name .= ' - ' . $name;

		$tree = new Tree($name, $property);

		foreach($this->parameterArray AS $parameter => $value)
			$tree->AddParameter($parameter, $value);

		return $tree;
	}
}